import pygame
import sys
import math

# 初始化 Pygame
pygame.init()

# 设置游戏窗口
WIDTH, HEIGHT = 400, 600
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("跳一跳小游戏")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)

# 玩家设置
player_radius = 20
player_x = WIDTH // 2
player_y = HEIGHT - player_radius
player_vel = 0
jump_power = -20
GRAVITY = 1

# 平台设置
platform_width = 80
platform_height = 20
platform_x = WIDTH // 2 - platform_width // 2
platform_y = HEIGHT - 100

clock = pygame.time.Clock()

# 游戏主循环
run = True
while run:
    clock.tick(30)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            player_vel = jump_power

    # 玩家移动
    player_vel += GRAVITY
    player_y += player_vel

    # 碰撞检测
    if (player_x >= platform_x and player_x <= platform_x + platform_width and
        player_y + player_radius >= platform_y and player_y + player_radius <= platform_y + platform_height):
        player_vel = 0
        player_y = platform_y - player_radius

    # 绘制背景
    win.fill(WHITE)
    
    # 绘制玩家
    pygame.draw.circle(win, GREEN, (int(player_x), int(player_y)), player_radius)
    
    # 绘制平台
    pygame.draw.rect(win, BLACK, (platform_x, platform_y, platform_width, platform_height))
    
    pygame.display.update()

pygame.quit()
sys.exit()